package cn.edu.nju.jmeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * Hello world!
 * 
 */
public class App extends AbstractJavaSamplerClient {

	private String str = "";
	private static int random = 80+(int) (Math.random()*40);

	public void setupTest(JavaSamplerContext arg0) {
		this.str = arg0.getParameter("str", "abcdefg");
	}

	public Arguments getDefaultParameters() {
		Arguments params = new Arguments();
		params.addArgument("str", "");
		return params;
	}

	public SampleResult runTest(JavaSamplerContext arg0) {
		SampleResult results = new SampleResult();
		int len = 0;
		results.sampleStart();
		len = str.length();
		try {
			Thread.sleep(random);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (len % 2 == 0) {
			results.setSuccessful(false);
		} else {
			results.setSuccessful(true);
		}
		results.sampleEnd();
		return results;
	}

	public void teardownTest(JavaSamplerContext arg0) {
	}

	public static void main(String[] args) {
		App app = new App();
		Arguments arg = new Arguments();
		arg.addArgument("str", "HAHA!");
		app.runTest(new JavaSamplerContext(arg));
	}

}
